#! /usr/bin/python
# -*- coding: utf-8 -*-

import os
from google.appengine.ext.webapp import template
import cgi
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
import urllib
import conf
import utils
import permission
import adebook

class MainPage(webapp.RequestHandler):
    def get(self):
        cookieDict = utils.getXnCookieDict(self.request.cookies)
        xnuid = cookieDict.get('user')
        is_logged_in = utils.validateXnCookies(cookieDict, conf.app_secret)
        callbackUrl = utils.getUrlPathQuery(self.request)
        if not is_logged_in or not permission.hasResetCounterPermission(xnuid):
            self.redirect("/login?" + urllib.urlencode({'url':callbackUrl}))
            return None

        ebookList = adebook.listAllAdebooks(0, 500)

        template_values = {
            'staticUrl': conf.staticUrl,
            'jsVersion': conf.jsVersion,
            'cssVersion': conf.cssVersion,
            'xnuid': xnuid,
            'nav_selected': 'ebook',
            'ebookList': ebookList
        }
        path = os.path.join(os.path.dirname(__file__), 'pages/admin/listebooks.html')
        self.response.out.write(template.render(path, template_values))

application = webapp.WSGIApplication(
                                     [('/admin/listebooks', MainPage)],
                                     debug=True)
def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
